﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using woaC2.LogicLib.DataContract.Output;
using woaC2.SocketLib;
using Newtonsoft.Json;
using woaC2.LogicLib.DataContract.Input;
using woaC2.Common;

namespace woaPoker.LogicLib.LogicCtrl
{
    class StartGameLgCtrl
    {
        //chuyen trang thai sang true
        public List<InfoPlayerStartGameOutDC> StartGame(Guid sessionId)
        {
            //var b = new Flag();
            //b.fg = true;
            //MathRegistry.instance.SetStatus(sessionId, true);
            //return b;
            //------------------------------------------------------------------------------
            //Random random1 = new Random();
            //List<InfoPlayerStartGameOutDC> list1 = new List<InfoPlayerStartGameOutDC>();
            //int[] beginX1 = new int[] { 7, 13, 27 };
            //InfoPlayerStartGameOutDC b = new InfoPlayerStartGameOutDC();
            //b.sessionId = sessionIdMatch;
            //b.beginX = 7;
            //b.beginY = 3;
            //b.userName = "Guest";
            //b.team = 2;
            //list1.Add(b);
            //for (int i = 1; i <= 2; i++)
            //{
            //    InfoPlayerStartGameOutDC a = new InfoPlayerStartGameOutDC();
            //    a.sessionId = Guid.NewGuid();
            //    a.beginX = beginX1[i];
            //    a.beginY = 3;
            //    //a.sessionId = player.client.SessionID;
            //    a.userName = "Guest";
            //    a.team = 2;
            //    list1.Add(a);
            //}
            //for (int i = 0; i < 3; i++)
            //{
            //    InfoPlayerStartGameOutDC a = new InfoPlayerStartGameOutDC();
            //    a.sessionId = Guid.NewGuid();
            //    a.beginX = beginX1[i];
            //    a.beginY = 55;
            //    //a.sessionId = player.client.SessionID;
            //    a.userName = "Guest";
            //    a.team = 1;
            //    list1.Add(a);
            //}
            //return list1;
            //-----------------=--------------------------------------------------------
            List<InfoPlayerStartGameOutDC> list = new List<InfoPlayerStartGameOutDC>();
            if (MathRegistry.instance.Contains(sessionId))
            {
                MathRegistry.instance.SetStatus(sessionId, true);

                Match match = MathRegistry.instance.GetMatchById(sessionId);
                Random random = new Random();
                int[] beginX = new int[] { 7, 13, 27 };
                int i = 0;
                foreach (Player player in match.listPlayer1)
                {
                    InfoPlayerStartGameOutDC a = new InfoPlayerStartGameOutDC();
                    a.beginX = beginX[i];
                    a.beginY = 55;
                    i++;
                    a.sessionId = player.client.SessionID;
                    a.userName = player.userName;
                    a.team = 1;
                    list.Add(a);
                }
                i = 0;
                foreach (Player player in match.listPlayer2)
                {
                    InfoPlayerStartGameOutDC a = new InfoPlayerStartGameOutDC();
                    a.beginX = beginX[i];
                    a.beginY = 3;
                    i++;
                    a.sessionId = player.client.SessionID;
                    a.userName = player.userName;
                    a.team = 2;
                    list.Add(a);
                }

                var jsonOut = "mes105$"+JsonConvert.SerializeObject(list);
                var mesOut = FunctionCmn.StampMessage(jsonOut);
                SocketServer.Instance.Broadcast(match, sessionId, mesOut);
            }

            return list;
            
        }

        public ShootOutDC Shoot(ShootInDc shoot,Guid sessionId)
        {

            Match match = MathRegistry.instance.GetMatchById(sessionId);
            BroadcastShootOutDC shootOut = new BroadcastShootOutDC();
            shootOut.sessionId = sessionId;
            shootOut.x = shoot.beginX;
            shootOut.y = shoot.beginY;
            shootOut.vector = shoot.arrow;
            var jsonOut = JsonConvert.SerializeObject(shootOut);
            var mesOut = FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.Broadcast(match, sessionId, mesOut);
            ShootOutDC a = new ShootOutDC();
            a.success = true;
            return a;
        }

        public MoveOutDC Move(MoveInDc move, Guid sessionId)
        {
            Match match = MathRegistry.instance.FindMatchByPlayerId(sessionId);
            BroadCastMoveOutDC moveOut = new BroadCastMoveOutDC();
            moveOut.sessionId = sessionId;
            moveOut.x = move.x;
            moveOut.y = move.y;
            moveOut.vector = move.movevector;
            var jsonOut = "mes106$"+JsonConvert.SerializeObject(moveOut);
            var mesOut = FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.Broadcast(match, sessionId, mesOut);
            MoveOutDC a = new MoveOutDC();
            a.succses = true;
            return a;

        }


    }
}
